<?php

use \Workerman\Worker;
use \Workerman\Connection\AsyncTcpConnection;

require_once __DIR__ . '/Autoloader.php';

$gateway = new Worker();
$gateway->count = 4;

$gateway->onWorkerStart = function ($worker) use($gateway) {
    $worker = new AsyncTcpConnection("ws://127.0.0.1:3000");
    $worker->onConnect = function ($con) {
        echo '连接成功';
    };
    $worker->onMessage = function ($con, $data) use ($gateway) {
        echo $data.'--------'.$gateway->id.PHP_EOL;
        $con->send($data);
    };
    $worker->onError = function ($con, $code, $msg) {
        echo $msg;
    };
    $worker->connect();
};

$gateway->onConnect = function ($connection) use ($gateway) {
    echo 'connect';

};

$gateway->onMessage = function ($connection, $data) use ($gateway) {
};


$gateway->onClose = function ($con) {
    echo 'close';
};
// 运行worker
Worker::runAll();